package com.hx_ai.nlp.simple.utils;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static sun.font.FontUtilities.isWindows;

/**
 * 天气属性
 */
@Getter
@Slf4j
public class GaodeWeatherProperties {

    private Map<String, String> gaodeWeather;
    private Map<String, Long> dateToDay;
    private List<String> negative;
    private List<String> keyword;

    private void setupGaodeWeather() {
        gaodeWeather = new HashMap<>();
        List<String> lines = new ArrayList<>();

        Charset charset = StandardCharsets.UTF_8;
        if (isWindows){
            charset = StandardCharsetsNew.GBK;
        }

        try {
            InputStreamLoadUtils.inputSteamLoad("/gaode_weather.txt", charset, lines);
            if (!lines.isEmpty()){
                for (String line : lines) {
                    String[] radio = line.split(",");
                    String key = radio[0];
                    if (StringUtils.isBlank(key)){
                        continue;
                    }
                    gaodeWeather.put(key, radio[1]);
                    if (StringUtils.endsWithAny(key, "省", "市", "区", "县", "州", "旗", "盟")){
                        String key2 = key.substring(0, key.length() - 1);
                        // 形如[北区]这种,去掉区后只有一个字,容易误解
                        if (StringUtils.length(key2) > 1){
                            gaodeWeather.put(key2, radio[1]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.info("===>setupRadios-解析txt文件异常, errMsg={}<===", e.getMessage());
            e.printStackTrace();
        }
    }

    private void setupKeyword(){
        String[] negativeWords = {"天气", "气候", "下雨", "晴天", "雨天", "刮风", "天晴", "台风", "多云", "太阳"};
        keyword = Arrays.asList(negativeWords);
    }

    private void setupNegative() {
        String[] negativeWords = {"不想", "不喜欢", "不要", "不爱", "别", "不能"};
        negative = Arrays.asList(negativeWords);
    }

    public void setupDate(){
        dateToDay = new HashMap<>();

        Date today = new Date();
        String[] dates = {"今天", "明天", "后天", "大后天", "今日", "明日", "后日", "大后日"};
        int[] dateId = {0, 1, 2, 3, 0, 1, 2, 3};
        for (int i = 0; i < dates.length; i++) {
            dateToDay.put(dates[i], today.getTime() + dateId[i] * 86400000);
        }

        String[] weekCh1 = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        String[] weekCh2 = {"礼拜天", "礼拜一", "礼拜二", "礼拜三", "礼拜四", "礼拜五", "礼拜六"};
        String[] weekCh3 = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
        for (int i = 0; i < weekCh1.length; i++) {
            Date dt = new Date();
            int dd = i <= dayOfWeek ? i - dayOfWeek : i - dayOfWeek - 7;
            dt.setTime(today.getTime() + dd * 86400000L);
            dateToDay.put(weekCh1[i], dt.getTime());
            dateToDay.put(weekCh2[i], dt.getTime());
            dateToDay.put(weekCh3[i], dt.getTime());
        }

    }

    public GaodeWeatherProperties() {
        setupGaodeWeather();
        setupNegative();
        setupDate();
        setupKeyword();
    }

}
